package com.eclipse.e20200125_xml_properties_注解.xml;



import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.List;

public class DOM解析 {
	public static void main(String[] args) throws Exception {

		//1.创建一个Document解析工厂
		DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();

		//2.从解析工厂中得到解析器
		DocumentBuilder db = dbf.newDocumentBuilder();
		//3.通过解析器，解析文档成为一个Document对象
		org.w3c.dom.Document document = db.parse("./src/main/java/com/eclipse/e20200125_xml_properties_注解/xml/config.xml");



		//获取根元素config
		Element elementRoot = document.getDocumentElement();
		NodeList config = elementRoot.getElementsByTagName("jdbc");

		for(int i=0;i<config.getLength();i++){
			//将每一个节点强转为一个element
			Element element = (Element) config.item(i);
			String title = element.getElementsByTagName("url").item(0).getFirstChild().getNodeValue();
			System.out.println(title);

			String author = element.getElementsByTagName("username").item(0).getFirstChild().getNodeValue();
			System.out.println(author);
			System.out.println("====================================");
		}

	}
}
